We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

$route->addPost gives: A dependency injection container is required to access the 'request' service

Hello, I'm in the beginning stages of writing an Angular/AJAX application with Phalcon. I have routing implemented and when I use:

$router->add("/ajax/promotions",[
    'controller' => 'noauthajax',
    'action' => 'promotions'
]);

everything works as it should, but when I change to:

$router->addPost("/ajax/promotions",[
    'controller' => 'noauthajax',
    'action' => 'promotions'
]);

I get the error: A dependency injection container is required to access the 'request' service

I'm guessing I'm missing a use or $di somewhere, but not sure why this is happening. I would really like to specify whether or not my requests are get or post.

Here is my $di config:

use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Mvc\Application;
use Phalcon\DI\FactoryDefault;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Security;
use Phalcon\Events\Manager as EventsManager;
use Phalcon\Session\Adapter\Files as SessionAdapter;

try {

    // Register an autoloader
    $loader = new Loader();
    $loader->registerDirs(array(
        '../app/controllers/',
        '../app/models/'
    ))->register();

    // Create a DI
    $di = new FactoryDefault();

    $di->set('dispatcher', function() use ($di) {
        $eventsManager = new EventsManager;
        /**
        * Check if the user is allowed to access certain action using the SecurityPlugin
        */
        $eventsManager->attach('dispatch:beforeDispatch', new SecurityPlugin);
        /**
        * Handle exceptions and not-found exceptions using NotFoundPlugin
        */
        $eventsManager->attach('dispatch:beforeException', new NotFoundPlugin);
        $dispatcher = new Dispatcher;
        $dispatcher->setEventsManager($eventsManager);
        return $dispatcher;
    });

    $di->set('security', function(){
        $security = new Security();
        //Set the password hashing factor to 12 rounds
        $security->setWorkFactor(12);
        return $security;
    }, true);

    // Setup the database service
    $di->set('db', function(){
        return new DbAdapter(array(
            "host"     => "****",
            "username" => "****",
            "password" => "****",
            "dbname"   => "****"
        ));
    });

    /**
    * Start the session the first time some component request the session service
    */
    $di->set('session', function() {
        $session = new SessionAdapter();
        $session->start();
        return $session;
    });

    $di->set('router',function() {
        require '../app/routes.php';
        return $router;
    });

    // Setup the view component
    $di->set('view', function(){
        $view = new View();
        $view->setViewsDir('../app/views/');
        return $view;
    });

    $di->set('url', function(){
        $url = new UrlProvider();
        $url->setBaseUri('/');
        return $url;
    });

    // Handle the request
    $application = new Application($di);

    echo $application->handle()->getContent();

} catch(\Exception $e) {
     echo "PhalconException: ", $e->getMessage();
}


4.0k
Accepted
answer
edited Jun '15

Try this. In $di config

// from
$di->set('router',function() {
        require '../app/routes.php';
        return $router;
    });
// to
$di->set('router',function () use ($di) {
        require '../app/routes.php';
        return $router;
    });

and in ../app/routes.php add line:

$router->setDI($di);


17.5k

Excellent! Thank you!